<template>
  <div style="margin-top:10px">
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="handleEdit" icon="edit" type="" v-if="selectedRowKeys.length == 1">编辑</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="enableSlot" slot-scope="enable">
          <a-icon v-if="!enable" type="close-circle" theme="twoTone"/>
          <a-icon v-if="enable" type="check-circle" theme="twoTone" two-tone-color="#52c41a"/>
        </span>
        <span slot="titleSlot" slot-scope="title, record">
          <span>{{title}}&nbsp;</span>
          <a-icon v-if="record.hideTitle" type="eye-invisible" theme="twoTone" title="隐藏标题"/>
          <a-icon v-if="!record.hideTitle" type="eye" theme="twoTone" two-tone-color="#52c41a" title="显示标题"/>
        </span>
        <span slot="displaySlot" slot-scope="isDisplay, record">
          <span v-if="isDisplay">启动加载</span>
          <span v-if="!isDisplay">手动加载</span>
        </span>
      </a-table>
    </div>

    <label-drawer ref="labelDrawer" @ok="modalFormOk" :deviceModelBy="deviceModelId"></label-drawer>
  </div>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import LabelDrawer from './LabelDrawer.vue'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import { getAction } from '@/api/manage'

export default {
  name: 'DeviceLabelList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    LabelDrawer
  },
  props: {
    deviceModelId: {
      type: String,
      default: false,
      required: true
    }
  },
  data() {
    return {
      description: '设备标签管理页面',
      dataSource: [],
      // 表头
      columns: [
        {
          title: '状态',
          align: 'center',
          dataIndex: 'enable',
          scopedSlots: { customRender: 'enableSlot' }
        },
        {
          title: '标题',
          align: 'center',
          dataIndex: 'title',
          scopedSlots: { customRender: 'titleSlot' }
        },
        {
          title: '默认显示',
          align: 'center',
          dataIndex: 'isDisplay',
          scopedSlots: { customRender: 'displaySlot' }
        },
        {
          title: '触发方式',
          align: 'center',
          dataIndex: 'defaultTriggerText'
        },
        {
          title: '关闭方式',
          align: 'center',
          dataIndex: 'closeMethodText'
        }
      ],
      url: {
        list: '/device/label/queryByModelId',
        getLabelById: '/device/label/queryById',
        delete: '/device/label/delete',
        deleteBatch: '/device/label/deleteBatch',
        exportXlsUrl: '/device/label/exportXls',
        importExcelUrl: 'device/label/importExcel'
      },
      dictOptions: {}
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    loadData() {
      let that = this
      getAction(this.url.list, { modelId: this.deviceModelId }).then(res => {
        if (res.success) {
          that.dataSource = res.result || []
        } else {
          that.dataSource = []
        }
      })
    },
    handleAdd() {
      this.$refs.labelDrawer.title = '添加标签'
      this.$refs.labelDrawer.add()
    },
    handleEdit() {
      let labelId = this.selectedRowKeys[0]
      this.$refs.labelDrawer.title = '编辑标签'
      getAction(this.url.getLabelById, { id: labelId }).then(res => {
        console.log(res)
        if (res.success) {
          this.$refs.labelDrawer.edit(res.result)
        }
      })
    },
    initDictConfig() {}
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
